<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'mcode', validatorRules.labelTemplateNo]"
                placeholder="请输入产品编码"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'mname', validatorRules.labelTemplateName]"
                placeholder="请输入产品名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="产品系列" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'productLine', validatorRules.labelTemplateLength]"
                placeholder="请输入产品系列"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="工艺路线编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'processCoding', validatorRules.labelTemplateDescrtion]"
                placeholder="请输入工艺路线编码"
              ></p-input>
            </p-form-item>
          </p-col>
            <p-col :span="24">
            <p-form-item label="工艺路线名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'processName', validatorRules.labelTemplateDescrtion]"
                placeholder="请输入工艺路线名称"
              ></p-input>
            </p-form-item>
          </p-col>
            <p-col :span="24">
            <p-form-item label="工艺路线类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'processType', validatorRules.labelTemplateDescrtion]"
                placeholder="请输入工艺路线类型"
              ></p-input>
            </p-form-item>
          </p-col>
           <p-col :span="24">
            <p-form-item label="审核" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <p-select default-value="1" v-decorator="['processPt']">
                  <p-select-option value="1">发行</p-select-option>
                  <p-select-option value="2">审批中</p-select-option>
                  <p-select-option value="3">待审批</p-select-option>
                  <p-select-option value="4">被驳回</p-select-option>
                </p-select>
            </p-form-item>
          </p-col>
            <p-col :span="0">
            <p-form-item label="审核" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="['id']"
              ></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, putAction, api } from "@/api/manage";
import fSwitch from "@/components/switch";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";
import { validateDuplicateValue } from "@/utils/util";
import JSelectDepartMes from "@/components/jeecgbiz/JSelectDepartMes";
import JDictSelectTag from "@/components/dict/JDictSelectTag";

export default {
  name: "Prodeceridx",
  mixins: [SubmitOnchangMixin],
  components: {
    JSelectDepartMes,
    JDictSelectTag,
    fSwitch,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        labelTemplateNo: {
          rules: [{ required: true, message: "请输入标签模板编号!" }],
        },
        labelTemplateName: {
          rules: [{ required: true, message: "请输入标签模板名称!" }],
        },
        labelTemplateLength: {
          rules: [{ required: true, message: "请输入标签模板尺寸!" }],
        },
        labelTemplateDescrtion: { rules: [] },
        labelType: { rules: [{ required: true, message: "请输入标签类型!" }] },
        dptname: { rules: [{ required: true, message: "请输入责任部门!" }] },
        labelColor: {
          rules: [{ required: true, message: "请输入标签纸颜色!" }],
        },
        ribbonsType: { rules: [] },
        printerType: {
          rules: [{ required: true, message: "请输入打印机型号!" }],
        },
        containSn: {
          rules: [{ required: true, message: "请输入是否包含流水号!" }],
        },
        processStatus: { rules: [{ required: true, message: "请输入状态!" }] },
        remark: { rules: [] },
      },
      url: {
        add: "/mes-craft/craft/mesPeProcessRoute/add",
        edit: "/mes-craft/craft/mesPeProcessRoute/add",
      },
    };
  },
  created() {},
  methods: {
    add() {
      this.edit({});
      this.getRuleCodeValue();
    },
    getRuleCodeValue() {
      let param = {
        commonFormData: this.model,
        rules: [
          { ruleCode: "mes_label_template_no", columnName: "labelTemplateNo" },
        ],
      };

      putAction(api.ruleBaseURL, param).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          console.log("生成的编码：" + res.data);
          const item = res.data;
          this.form.setFieldsValue(pick(item, Object.keys(item)));
        }
      });
    },

    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "mname",
            "mcode",
            "productLine"
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let formData = Object.assign(this.model, values);
            this.formData = Object.assign(this.model, values);
            //新增触发
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
              if(this.EnhanceJS.afterAdd){
                //新增之前
                let Obj = eval(this.EnhanceJS.afterAdd)
                new Obj(this)
              }else{
                formData.routerStatus='1'
                console.log("表单提交数据",formData)
                that.modalFrom(httpurl,formData,method)
              }
            }else{
              //编辑触发
              httpurl+=this.url.edit;
              method = 'post';
              if(this.EnhanceJS.afterEdit){
               //编辑之前
                let Obj = eval(this.EnhanceJS.afterEdit)
                new Obj(this)
              }else{
                formData.routerStatus='2'
                console.log("表单提交数据",formData)
                that.modalFrom(httpurl,formData,method)
              }
            }
        }
        console.log(values);
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "createBy",
          "createTime",
          "updateBy",
          "updateTime",
          "labelTemplateNo",
          "labelTemplateName",
          "labelTemplateLength",
          "labelTemplateDescrtion",
          "labelType",
          "dptname",
          "labelColor",
          "ribbonsType",
          "printerType",
          "containSn",
          "status",
          "remark"
        )
      );
    },
  },
};
</script>